package com.passionfruit.module.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.passionfruit.common.exception.PassionException;
import com.passionfruit.common.utils.Constant;
import com.passionfruit.module.entity.SysRole;
import com.passionfruit.module.entity.SysRoleMenu;
import com.passionfruit.module.entity.SysUserRole;
import com.passionfruit.module.mapper.SysRoleMapper;
import com.passionfruit.module.service.ISysRoleMenuService;
import com.passionfruit.module.service.ISysRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.passionfruit.module.service.ISysUserRoleService;
import com.passionfruit.module.service.ISysUserService;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author weirdor
 * @since 2017-10-31
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    @Transactional
    public void save(SysRole role) {
        role.setCreateTime(new Date());
        super.insert(role);

        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional
    public void update(SysRole role) {
        super.insertOrUpdate(role);

        //检查权限是否越权
        checkPrems(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    public List<Long> queryRoleIdList(Long createUserId) {
        List<Long> longList = new ArrayList<>();
        EntityWrapper<SysRole> sysRoleEntityWrapper = new EntityWrapper<>();
        sysRoleEntityWrapper.eq("create_user_id", createUserId);
        List<SysRole> sysUserRoles = selectList(sysRoleEntityWrapper);
        sysUserRoles.forEach(k -> {
            longList.add(k.getRoleId());
        });
        return longList;
    }

    @Override
    public void deleteBatch(Long[] roleIds) {
        super.deleteBatchIds(CollectionUtil.newArrayList(roleIds));
        EntityWrapper<SysRoleMenu> ew = new EntityWrapper<>();
        ew.where("role_id").in("role_id", roleIds);
        sysRoleMenuService.delete(ew);
        EntityWrapper<SysUserRole> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("role_id").in("role_id", roleIds);
        sysUserRoleService.delete(entityWrapper);
    }


    /**
     * 检查权限是否越权
     */
    private void checkPrems(SysRole role) {
        //如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
        if (role.getCreateUserId() == Constant.SUPER_ADMIN) {
            return;
        }

        //查询用户所拥有的菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(role.getCreateUserId());

        //判断是否越权
        if (!menuIdList.containsAll(role.getMenuIdList())) {
            throw new PassionException("新增角色的权限，已超出你的权限范围");
        }
    }
}
